package spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.task;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_manager.tasks_manager.service.AllotTaskItemService;
import spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.TaskItemAllotTask;
import spring.cloud.tasks.tasks_manager.tasks_manager.task_item_allot.executor.Executor;

import java.util.ArrayList;
import java.util.List;

/**
 * 作业启用，获取该作业的shards，注意要过滤不能运行该作业的executors
 */
@Slf4j
@Setter
@Getter
public class TaskEnableTaskItemAllotTask extends TaskItemAllotTask {

    private String taskId;

    public TaskEnableTaskItemAllotTask(AllotTaskItemService allotTaskItemService, String taskId) {
        super(allotTaskItemService);
        this.taskId = taskId;
    }

    @Override
    protected void logStartInfo() {
        log.info("Execute the {} with {} enable", this.getClass().getSimpleName(), taskId);
    }

    @Override
    protected List<String> notifyEnableTaskIdListPrior() {
        List<String> taskIdList = new ArrayList<>();
        taskIdList.add(taskId);
        return taskIdList;
    }

    @Override
    protected boolean pick(List<String> taskIdList,//
                           List<String> enableTaskIdList,//
                           List<TaskItem> taskItemList,//
                           List<Executor> onlineExecutorList,//
                           List<Executor> onlineOwnTrafficExecutorList) throws Exception {
        //先移除清理
        removeTaskItemList(onlineOwnTrafficExecutorList, taskId);
        //获取支持该task的Executor
        refreshExecutorTaskIdList(onlineExecutorList, taskId);
        //重新创建
        taskItemList.addAll(createTaskItemList(taskId));

        return true;
    }

}
